package org.zhaozhen.basic.util;


import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zhaozhen.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 要使用容器管理的类，需要拿到容器，在容器中使用
 *
 * 根据token获取
 */
public class LoginContext {

    public static Logininfo getCurrentLogin(HttpServletRequest request){
        String token = request.getHeader("token");
        if (token==null){
            return null;
        }
        //获取容器
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
        //获取容器中的类
        RedisTemplate redisTemplate = (RedisTemplate)webApplicationContext.getBean("redisTemplate");
        //根据token获取logininfo
        Logininfo logininfo  = (Logininfo)redisTemplate.opsForValue().get(token);
        if (logininfo==null){
            return null;
        }
        return logininfo;
    }
}
